import store from '../store/index.js';
const whiteList = ['/pages/login/login'];

//检查地址白名单
function checkWhite(url) {
	const path = url.split('?')[0];
	return whiteList.indexOf(path) != -1;
}
//页面跳转验证拦截器
let list = ['navigateTo', 'redirectTo', 'relaunch', 'switchTab'];
list.forEach((item) => {
	uni.addInterceptor(item, {
		invoke(args) {
			// request 触发前拼接 url
			// console.log('触发前拼接args', args);
			// console.log('store.getters.getUserInfo()', store.getters.getUserInfo());
			if (args.url == '/pages/login/login' && store.getters.getUserInfo()) {
				// console.log('拦截-false,禁止去login 页');
				uni.switchTab({
					url: '/pages/flow/flow'
				});
				return false;
			}
			if (args.url != '/pages/login/login' && !store.getters.getUserInfo()) {
				// console.log('该跳转去login');
				uni.showToast({
					icon: 'none',
					title: '请先登录',
					duration: 2000
				});
				uni.reLaunch({
					url: '/pages/login/login'
				});
				return false;
			}
			return true;
		},
		success(args) {
			// 请求成功后，修改code值为1
			// args.data.code = 1
		},
		fail(err) {
			// 失败回调拦截
			console.log('失败回调拦截-interceptor-fail', err);
		},
		complete(res) {
			//完成回调拦截
			console.log('完成回调拦截-interceptor-complete', res);
		}
	});
});
